<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="../window/css/window.css" rel="stylesheet" />
    <link href="../window/css/print.css" rel="stylesheet" />
    <link href="../source/css/base.css" rel="stylesheet" />
    <link href="../source/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
    <link href="../source/css/bootstrap-select.min.css" rel="stylesheet" />
    <link href="../window/css/livecoverage_win.css" rel="stylesheet" />
    <script src="../source/js/jquery-1.9.1.min.js"></script>
    <script src="../source/layer/layer.js"></script>
    <script src="../source/js/jquery.qrcode.min.js"></script>
    <script src="../source/js/JsBarcode.all.min.js"></script>
    <script src="../source/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="../source/js/bootstrap-select.min.js"></script>
    <script src="../source/js/common_url.js"></script>
    <script src="../source/js/util.js"></script>

    <title>手动录入</title>
    <!--<style>
        #print {width: 380px; height: 600px; margin: 0 auto;}
    </style>-->

    <style>
        .bootstrap-select:not([class*=col-]):not([class*=form-control]):not(.input-group-btn) {
            width: 70% !important;
            height: 38px !important;
        }
        .lists{
            width: 400px;
            margin: 60px 0 0 0;
        }
        .list{
            /*font-size: 16px;*/
            position:relative;
            width: 370px;
            height: 36px;
            line-height: 36px;
            margin: 30px 0 5px 0 ;
            padding-left: 30px;
        }
        .lists .list #sdlr_patName, .lists .list #sdlr_queueName{
            width: 236px;
            height: 38px;
            line-height: 38px;
            border: 1px solid #c0c0c0;
            background: #fff;
            display: inline-block;
            padding-left: 0px;
            /*margin-top: -2px;*/
            text-indent: 2px;

        }
        .lists .listq{
            margin: 20px 0 5px 0 ;
        }
        .field-name {
            display: inline-block;
            width: 90px;
            /*margin-left: 20px;*/
            text-align: right;
            color: #7E807F;
        }
        .required-ast::after {
            content: '*';
            color: red;
            padding: 0 10px 0 0px;
        }

        .bootstrap-select:not([class*=col-]):not([class*=form-control]):not(.input-group-btn) {
            /*width: 100%;*/
            height: 38px !important;
        }
        .bootstrap-select:not([class*=col-]):not([class*=form-control]):not(.input-group-btn):hover {
            background: #fff;
        }

        .btn-default:active:hover, .btn-default.active:hover, .open > .dropdown-toggle.btn-default:hover{
             background-color: #fff;
             border-color: #9d9d9d;
        }
        .bootstrap-select .dropdown-toggle:focus, .bootstrap-select>select.mobile-device:focus+.dropdown-toggle {
            outline: thin dotted #fff!important;
            outline: 0px auto -webkit-focus-ring-color!important;
            outline-offset: 0px;
        }
        .btn {
            height: 34px;
            line-height: 34px;
            margin: 0;
            padding: 0;
            padding-left: 0px;
            border-radius: 0px;
            outline: none;
            margin-right: 0px;
        }
        input.error, select.error .errorColor{
            background-color: #fbe2e2;
            border-color: #c66161;
            color: #c00;
        }
        label.error {
            position: absolute;
            /*right: 18px;*/
            top: 0px;
            left: 0px;
            color: #ef392b;
            font-size: 14px;
        }
        .msg_content{
            margin-left: 125px;
        }
    </style>
</head>
<body>

<!-- 查询就诊号 浮层-->
<div class="lists" id="d_query">
    <form id="verifyInfo" action="/" class="form form-horizontal" method="post" >
        <div class="list" id="manualEntering">
            <label class="required-ast field-name">患者姓名：&nbsp;&nbsp;</label>
            <input type="text" id="sdlr_patName" name="sdlr_patName" value=""  /></span>
        </div>
        <span class="msg_content" id="msg-name">&nbsp;</span>
        <div class="list listq" >
            <label class="required-ast field-name" >队列名称：&nbsp;&nbsp;</label>
            <select id="sdlr_queueName" name="sdlr_queueName" class="selectpicker">
                <option value="1" data-registerType="1" data-registerObjKey="660ba180828b4f5bbeef183b27a9da17_3">2人普通队列</option>
                <option value="2" data-registerType="2" data-registerObjKey="df4249e42d6547df83883cd7f9853e86_2">刘医生专家队列</option>
            </select>
        </div>
        <span class="msg_content" id="msg-queue">&nbsp;</span>
        <div class="d_bottom_bd">
            <input class="but_ok" type="button" value="确定" onclick="manualEntering();" />
        </div>
    </form>
</div>
<div id="print" style="display: none;width: 70mm; height: 70mm;">
    <table >
        <tr style="height: 6mm">
            <td colspan="2" style="text-align: center"><h2 style="font-family:'SimSun'; font-weight:900; margin-top: 0px;
    margin-bottom: 5px;"><span id="hospitalName"></span>报到凭证</h2></td>
        </tr>
        <tr>
            <td colspan="2" style="width: 100%; height: 0; border: 0; border-top: 1px solid #000; line-height: 0;margin-bottom: 2px;"></td>
        </tr>
        <tr>
            <td colspan="2" align="center" valign="middle" style="width: 100%; height: 5mm">
                <img id="lineCode" width="300px" height="40px" align="center" src="lineCodeTag" />
            </td>
        </tr>
        <tr style="height: 5mm">
            <td style="width: 40%; text-align: right; font-size:20px; font-weight:900;">就诊号:</td>
            <td style="text-align: left; font-weight:700; font-size:20px;"><label id="registerId" /></td>
        </tr>
        <!--<tr style="height: 7mm">
            <td style="width: 40%; text-align: right; font-size:20px; font-weight:900;">患者ID:</td>
            <td style="text-align: left; font-size:20px;"><label id="patientId" /></td>
        </tr>-->
        <tr style="height: 5mm">
            <td style="width: 40%; text-align: right; font-size:20px; font-weight:900;">患者姓名:</td>
            <td style="text-align: left; font-size:20px;"><label id="patientName" /></td>
        </tr>
        <tr style="height: 5mm">
            <td style="width: 40%; text-align: right; font-size:20px; font-weight:900;">挂号科室:</td>
            <td style="text-align: left; font-size:20px;"><label id="deptName" /></td>
        </tr>
        <tr style="height: 5mm">
            <td style="width: 40%; text-align: right; font-size:20px; font-weight:900;">号别:</td>
            <td style="text-align: left; font-size:20px;"><label id="numType" /></td>
        </tr>
        <!--<tr style="height: 7mm">
            <td style="width: 40%; text-align: right; font-size:20px; font-weight:900;">午别:</td>
            <td style="text-align: left; font-size:20px;"><label id="wuBie" /></td>
        </tr>-->
        <tr style="height: 5mm">
            <td style="width: 40%; text-align: right; font-size:20px; font-weight:900;">挂号时间:</td>
            <td style="text-align: left; font-size:20px;"><label id="registerTime" /></td>
        </tr>
        <tr style="height: 5mm">
            <td style="width: 40%; text-align: right; font-size:20px; font-weight:900;">报到时间:</td>
            <td style="text-align: left; font-size:20px;"><label id="arrivalTime" /></td>
        </tr>
        <tr>
            <td colspan="2" style="width: 100%; height: 0; border: 0; border-top: 1px solid #000;line-height: 0;margin-bottom: 2px;"></td>
        </tr>
        <tr>
            <td colspan="2" style="width: 100%; text-align: left; font-size:22px; font-weight:900;">
                您前面还有<label id="prePatientNum"></label>人，请在候诊区耐心等待叫号！
            </td>
        </tr>
        <!--<tr>
            <td colspan="2" align="center" valign="middle" id="imgCode"><img id="imgCodeId" src="imgCodeTag" /></td>
        </tr>-->
    </table>
</div>
<script type="text/javascript">
    //手动录入
    var manuallyUrl = STSUrl +  "/Triage/manualEntryPatientInfo";
    //手动报到
    var queryPatBdInfoUrl = STSUrl + "/sign/initSign";
    //初诊报到
    var fstBdUrl = STSUrl + "/sign/firstSign";
    //手动分诊
    var manualTriageUrl = STSUrl + "/sign/manualTriage";
    //分诊台ID
    var _triageId = getDeviceId();
    //医院ID
    var _hospiId = getHospitalId();
    //登录用户ID(护士)
    var userId = getLoginUserId();
    //验证是否为数字
    var patternNumber = new RegExp(/(.+)?\d(.+)?/);
    var judgeSpacing = new RegExp(/\s/);
    $(function () {
        $('#sdlr_queueName').selectpicker('refresh');
        $('#sdlr_queueName').selectpicker('render');
        // initFormValidata();
        $("#sdlr_patName").on("input", function () {
            //验证输入的患者姓名
           patientInfo();
        });
    });
    //验证患者姓名，汉字，字母格式，限定6个字
    function patientInfo(){
        var _name = $("#sdlr_patName").val();
        if (_name.length > 0 && _name.length< 7) {
            $("#msg-name").html("&nbsp;");
            $("#sdlr_patName").css("border", '');
        } else if(_name.length > 6){
            $("#msg-name").html("姓名长度不能大于6位");
            $("#msg-name").css({ "color": "red", "font-weight": "bold" });
        } else if(_name.length<1){
            $("#msg-name").html("姓名不能为空");
            $("#msg-name").css({ "color": "red", "font-weight": "bold" });
        }
        if (pattern.test(_name)) {
            $("#msg-name").html("患者姓名不能包含特殊字符");
            $("#msg-name").css({ "color": "red", "font-weight": "bold" });
        }
        if (judgeSpacing.test(_name)) {
            $("#msg-name").html("患者姓名不能包含空格");
            $("#msg-name").css({ "color": "red", "font-weight": "bold" });
        }
        //判断是否含数字
        if (patternNumber.test(_name)) {
            $("#msg-name").html("患者姓名不能包含数字");
            $("#msg-name").css({ "color": "red", "font-weight": "bold" });
        }
    }
    $(document).keypress(function (e) {
        if (e.which == 13) {
            manualEntering();
        }
    });
    function manualEntering() {
        var _name = $("#sdlr_patName").val();
        if (_name.length > 6){
            $("#sdlr_patName").css("border-color", '#c66161');
            return false;
        } else if(_name.length < 1){
            $("#msg-name").html("姓名不能为空");
            $("#sdlr_patName").css("border-color", '#c66161');
            $("#msg-name").css({ "color": "red", "font-weight": "bold" });
            return false;
        }
        if (pattern.test(_name)) {
            $("#sdlr_patName").css("border-color", '#c66161');
            return false;
        }
        //判断是否含数字
        if (patternNumber.test(_name)) {
            $("#sdlr_patName").css("border-color", '#c66161');
            return false;
        }
        //判断是否包含空格
        if (judgeSpacing.test(_name)) {
            $("#sdlr_patName").css("border-color", '#c66161');
            return false;
        }
       var _patientKey = rndNum();
       var _patientName = $("#sdlr_patName").val();
       var _registerObjName = $("#sdlr_queueName option:selected").text();
       var _registerType =$("#sdlr_queueName option:selected").attr("data-registerType");
       var _registerObjKey =$("#sdlr_queueName option:selected").attr("data-registerObjKey");
       // var _registerObjName =$("#sdlr_queueName option:selected").attr("data-registerObjName");
       var _timeType = 0;
       var sex = -1 ;
        // $("#d_query").hide();
        // 报到-浮层 隐藏
        // $("#d_bd").hide();
        // 选择号别-浮层 显示
        // $("#d_display").show();
        // 打印层 隐藏
        $("#print").hide();
        $.ajax({
            url: manuallyUrl,
            type: 'post',
            data: {
                patientKey: _patientKey, //患者key，随机生成19位
                patientName: _patientName,//患者name
                registerType: _registerType,//固定值
                registerObjKey:_registerObjKey,//固定值
                registerObjName:_registerObjName,//固定值
                timeType:_timeType,//固定值
                sex: sex
            }, success: function (d) {
                var dataInfo = JSON.parse(d);
                if(dataInfo.status == 0){
                    infosign(_patientKey);
                } else {
                    //报到异常的提示信息
                    layer.alert(dataInfo.desc);
                }
            }
        })
    }

    //手动分诊
    function infosign(patientKey){
        $.ajax({
            url: queryPatBdInfoUrl,
            type: 'post',
            data: {
                triageId: _triageId, //分诊台id
                patientKey: patientKey //患者就诊卡号
            },
            beforeSend: function () {
                layer.msg('查询中...');
            },
            complete: function () {
                //layer.closeAll();
            },
            success: function (d) {
                // console.log('sign/initSign:'+d);
                //alert(d);
                var dataInfo = JSON.parse(d);
                //错误信息
                if (dataInfo.status == '30003' || dataInfo.status == '20001' || dataInfo.status == '20000')  {
                    layer.closeAll();
                    layer.alert(dataInfo.desc, {icon: 2});
                }
                else if (dataInfo.status == '0') {//初诊报到
                    layer.closeAll();
                    $("#print").hide();
                    var _deptId = dataInfo.data[0].deptId;
                    var _deptName = dataInfo.data[0].deptName;
                    var _queues = dataInfo.data[0].queueInfo;
                    var _patientName = _queues[0].patientName;
                    var _patientKey = _queues[0].patientKey;
                    var _registerTime = _queues[0].registerTime;
                    var _id = _queues[0].id;
                }
                bd(_id,_patientName,_deptName,_registerTime,_patientKey) ;
            }
        })
    }
    //初诊报到
    function bd(id,patientName,deptName,registerTime,_patientKey) {
        var _deptName = deptName;
        var _registerTime = registerTime;
        var _registerObjName = $("#sdlr_queueName option:selected").text();
        var _registerObjKey =$("#sdlr_queueName option:selected").attr("data-registerObjKey");
        $.ajax({
            url: fstBdUrl,
            type: 'post',
            beforeSend: function () {
                // layer.msg('正在报到...');
                layer.alert('', { //点击确认按钮后出现的遮罩，防止等待数据请求的过程中多次点击确认按钮
                    closeBtn: 0,
                    btn: 0,
                    title: 0,
                    area: ['0px', '0px']
                })
            },
            data: {
                hospitalId: _hospiId,   //医院ID
                triageId: _triageId,   //分诊台ID
                registerObjKey: _registerObjKey, //号的标识
                registerObjName:_registerObjName,  //号的名称
                id: id  //患者ID
            },
            success: function (d) {
                console.log(d);
                var jsonObj = JSON.parse(d);
                if (jsonObj.status == '0') {
                    //初诊报到时 jsonObj.data 为：患者挂号id(就是挂号表里的id)
                    //复诊报到时 jsonObj.data 为：患者报到id(就是患者报到表的signId)
                    var signId = JSON.parse(d).data;
                    if (signId != null) {
                        //报到成功进行分诊
                        manualTriage(_registerObjKey,signId,_deptName,_registerTime,_patientKey);
                    }
                } else {
                    //报到异常的提示信息
                    layer.alert(jsonObj.desc);
                }
            }
        });
    }
    //手动分诊
    function manualTriage(registerObjKey,id,deptName,registerTime,patientKey) {
        var queueKey = registerObjKey;
        var deptNamePrint = deptName;
        var registerTimePrint = registerTime;
        var btnType = 3;
        var signId = id.signId;
        var signTime = id.signTime;
        var old_color_status = 3;
        var doctorId = "";
        var doctorName = "";
        $.ajax({
            url: manualTriageUrl,
            type: 'post',
            data: {
                hospitalId: _hospiId,
                editor: userId,
                signIds: signId,
                queueKey: queueKey,
                pretestStage: btnType,
                oldPretestStage: old_color_status,
                doctorId: doctorId,
                doctorName: doctorName
            }, success: function (d) {
                var jsonObj = JSON.parse(d);
                if (jsonObj.status == '0') {
                    // layer.alert(jsonObj.desc, {icon: 1});
                    if (jsonObj.data != null && jsonObj.data != '' && jsonObj.data.length > 0) {
                        // $("#hid_registerId").val(jsonObj.data[0].registerId);//就诊序号
                        // $("#hid_prePatientNum").val(jsonObj.data[0].prePatientNum);//排队人数
                        var registerId = jsonObj.data[0].registerId;
                        var prePatientNum = jsonObj.data[0].prePatientNum;

                        try {
                            // 打印报到凭证
                            //registerPrintByLodop();
                            // alert(registerId+"=="+deptNamePrint+"=="+prePatientNum+"=="+registerTimePrint+"=="+_patientKey);
                            registerPrintByCSharp(registerId,deptNamePrint,prePatientNum,registerTimePrint,patientKey,signTime);
                        } catch (error) {
                            layer.alert('未检测到打印机驱动：' + error, { icon: 2 });
                        }
                    }
                    layer.closeAll();
                    parent.layer.closeAll();
                } else {
                    layer.closeAll();
                    layer.alert(jsonObj.desc, { icon: 2 });
                }
            }
        });
    }
    //报到打印（通过LODOP定义打印格式）
    function registerPrintByCSharp(registerId,deptName,prePatientNum,registerTime,patientKey,signTime){
        //报到时间加1秒，服务器挂号时间有时晚于报到时间1秒
        var signTimeAdd = getNowFormatDate(signTime);
        var _patientName = $('#sdlr_patName').val();//患者姓名
        var _numType = $("#sdlr_queueName option:selected").text();//队列名字
        $("#registerId").html(registerId);
        $("#patientName").html(_patientName);
        $("#deptName").html(deptName);
        $("#numType").html(_numType.substr(0, _numType.length));
        $("#registerTime").html(registerTime);
        $("#arrivalTime").html(signTimeAdd);
        $("#prePatientNum").html(prePatientNum);

        // 获取printhtml内容
        var printData = document.all.item("print").innerHTML;
        var htmlText = "<!DOCTYPE html><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta charset=\"utf-8\" /><title></title></head><body>" +
            "<div id=\"print\" style=\"width: 70mm; height:135mm;left:15px;position:absolute\">" + printData + "</div></body></html>";

        // 通知csharp做打印
        PrintByCSharp(patientKey, htmlText);
    }

    //获取随机数
    function rndNum(){
        var timestamp=new Date().getTime();
        var rnd="";
        var n = 6;
        for(var i=0;i<n;i++){
            rnd+=Math.floor(Math.random()*10);
        }
        var randomPatientKey = rnd+timestamp;
        return randomPatientKey;
    }

    //报到时间加1秒，服务器时间挂号时间有时晚于报到时间
    function getNowFormatDate(signData) {
        var time= new Date(signData).getTime()+1000;
        var date = new Date(time);
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth()+1;
        var strDate = date.getDate();
        var strHours = date.getHours();
        var strMinutes = date.getMinutes();
        var strSeconds = date.getSeconds();
        var currentdate = date.getFullYear() + seperator1 + toDou(month) + seperator1 + toDou(strDate)
            + " " + toDou(strHours) + seperator2 + toDou(strMinutes) + seperator2 + toDou(strSeconds);
        return currentdate;
    }

    //时间补 0
    function toDou(num) {
        var num = num + "";
        return num = num.length > 1 ? num : 0 + num;
    }

</script>
</body>
</html>
